package com.ruoyi.peoplegroup.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.peoplegroup.domain.Researcherinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.peoplegroup.mapper.ResearchertypeMapper;
import com.ruoyi.peoplegroup.domain.Researchertype;
import com.ruoyi.peoplegroup.service.IResearchertypeService;

/**
 * 科研人员类型Service业务层处理
 * 
 * @author gw
 * @date 2025-01-08
 */
@Service
public class ResearchertypeServiceImpl implements IResearchertypeService 
{
    @Autowired
    private ResearchertypeMapper researchertypeMapper;

    /**
     * 查询科研人员类型
     * 
     * @param researcherTypeId 科研人员类型主键
     * @return 科研人员类型
     */
    @Override
    public Researchertype selectResearchertypeByResearcherTypeId(Long researcherTypeId)
    {
        return researchertypeMapper.selectResearchertypeByResearcherTypeId(researcherTypeId);
    }

    /**
     * 查询科研人员类型列表
     * 
     * @param researchertype 科研人员类型
     * @return 科研人员类型
     */
    @Override
    public List<Researchertype> selectResearchertypeList(Researchertype researchertype)
    {
        return researchertypeMapper.selectResearchertypeList(researchertype);
    }

    /**
     * 新增科研人员类型
     * 
     * @param researchertype 科研人员类型
     * @return 结果
     */
    @Override
    public int insertResearchertype(Researchertype researchertype)
    {
        researchertype.setCreatedBy(SecurityUtils.getUsername());
        return researchertypeMapper.insertResearchertype(researchertype);
    }

    /**
     * 修改科研人员类型
     * 
     * @param researchertype 科研人员类型
     * @return 结果
     */
    @Override
    public int updateResearchertype(Researchertype researchertype)
    {
        researchertype.setUpdatedBy(SecurityUtils.getUsername());
        return researchertypeMapper.updateResearchertype(researchertype);
    }

    /**
     * 批量删除科研人员类型
     * 
     * @param researcherTypeIds 需要删除的科研人员类型主键
     * @return 结果
     */
    @Override
    public int deleteResearchertypeByResearcherTypeIds(Long[] researcherTypeIds)
    {
        int updatedCount = 0;  // 计数器，记录成功更新的记录数

        for (Long id : researcherTypeIds) {
            // 获取合作单位信息
            Researchertype researchertype= selectResearchertypeByResearcherTypeId(id);

            if (researchertype != null) {
                // 设置删除相关信息
                researchertype.setDeletedBy(SecurityUtils.getUsername());
                researchertype.setDeletedAt(new Date());
                researchertype.setIsDeleted(1);  // 标记为已删除

                // 更新合作单位信息
                int result = researchertypeMapper.updateResearchertype(researchertype);
                if (result > 0) {
                    updatedCount++;  // 如果更新成功，计数器加1
                }
            }
        }

        return updatedCount;
    }

    /**
     * 删除科研人员类型信息
     * 
     * @param researcherTypeId 科研人员类型主键
     * @return 结果
     */
    @Override
    public int deleteResearchertypeByResearcherTypeId(Long researcherTypeId)
    {
        return researchertypeMapper.deleteResearchertypeByResearcherTypeId(researcherTypeId);
    }
}
